﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace Expander
{
    [TemplatePart(Name = "PART_HeaderToggleButton", Type = typeof(ToggleButton))]
    [TemplateVisualState(Name = "Expanded", GroupName = "PresentationStates")]
    [TemplateVisualState(Name = "Collapsed", GroupName = "PresentationStates")]
    public class ExpanderControl : ContentControl, INotifyPropertyChanged
    {
        #region Fields

        private ToggleButton _headerButton = null;         
   
        public event EventHandler<EventArgs> Expanded;
        public event EventHandler<EventArgs> Collapsed;

        #endregion Fields
        #region Constructors

        public ExpanderControl()
        {
            this.DefaultStyleKey = typeof(ExpanderControl);            
        }

        #endregion Constructors
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion INotifyPropertyChanged
        #region Properties

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ExpanderControl), new PropertyMetadata(OnHeaderPropertyChanged));      
        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        private static void OnHeaderPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ExpanderControl control = sender as ExpanderControl;
            if (control != null)
            {
                if (control._headerButton != null)
                {
                    control._headerButton.Content = e.NewValue;
                    control.NotifyPropertyChanged("Header");
                }
            }
        }

        #endregion Properties
        #region Methods

        public override void OnApplyTemplate()
        {
            this._headerButton = this.GetTemplateChild("PART_HeaderToggleButton") as ToggleButton;
            if (_headerButton == null)
            {
                throw new NullReferenceException("Could not find templated PART: PART_HeaderToggleButton");
            }
            _headerButton.Content = Header;
            _headerButton.Checked += new RoutedEventHandler(OnHeaderButtonChecked);
            _headerButton.Unchecked += new RoutedEventHandler(OnHeaderButtonUnchecked);            
        }

        private void OnHeaderButtonUnchecked(object sender, RoutedEventArgs e)
        {            
            VisualStateManager.GoToState(this, "Collapsed", true);
            OnCollapsed();
        }

        private void OnHeaderButtonChecked(object sender, RoutedEventArgs e)
        {         
            VisualStateManager.GoToState(this, "Expanded", true);
            OnExpanded();
        }

        protected void OnExpanded()
        {
            if (Expanded != null)
            {
                Expanded(this, EventArgs.Empty);
            }
        }

        protected void OnCollapsed()
        {
            if (Collapsed != null)
            {
                Collapsed(this, EventArgs.Empty);
            }
        }

        #endregion Methods
    }
}
